﻿/*
 * 作者：新生命开发团队（http://www.newlifex.com/）
 * 
 * 版权：版权所有 (C) 新生命开发团队 2002-2014
 * 
 * 修改：海洋饼干（cuteant@outlook.com）
*/

using CuteAnt.Xml;

namespace CuteAnt.Serialization
{
	/// <summary>读写器服务。将来可以改为对象容器支持</summary>
	internal static class RWService
	{
		public static IReader CreateReader(RWKinds kind)
		{
			switch (kind)
			{
				case RWKinds.Binary:
					return new BinaryReaderX();

				case RWKinds.Xml:
					return new XmlReaderX();

				case RWKinds.Json:
					return new JsonReader();

				default:
					break;
			}
			return null;
		}

		public static IWriter CreateWriter(RWKinds kind)
		{
			switch (kind)
			{
				case RWKinds.Binary:
					return new BinaryWriterX();

				case RWKinds.Xml:
					return new XmlWriterX();

				case RWKinds.Json:
					return new JsonWriter();

				default:
					break;
			}
			return null;
		}

		public static RWKinds GetKind(this IReaderWriter rw)
		{
			var type = rw.GetType();
			if (type == typeof(BinaryReaderX)) { return RWKinds.Binary; }
			if (type == typeof(BinaryWriterX)) { return RWKinds.Binary; }
			if (type == typeof(XmlReaderX)) { return RWKinds.Xml; }
			if (type == typeof(XmlWriterX)) { return RWKinds.Xml; }
			if (type == typeof(JsonReader)) { return RWKinds.Json; }
			if (type == typeof(JsonWriter)) { return RWKinds.Json; }

			throw new HmExceptionBase("未识别的读写器类型{0}！", type);
		}
	}
}